{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding a Directory Trigger to a Lattice\n",
    "\n",
    "This example illustrates how to use a `covalent.trigger.DirTrigger` to trigger workflow dispatches automatically whenever a file in a directory is modified.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "1. Import Covalent, the trigger, and the `Path` library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "from covalent.triggers import DirTrigger\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Create a file in the current directory named `my_text_file.txt` and populate it with some numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"my_text_file.txt\", \"w\") as f:\n",
    "    for i in range(10):\n",
    "        f.write(f\"{i}\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Procedure\n",
    "\n",
    "1. Create a directory trigger. A `DirTrigger` object performs a trigger action whenever a file is modified in the current directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_trigger = DirTrigger(dir_path=str(Path(\".\").resolve()), event_names=\"modified\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Create a simple workflow called `my_workflow` and assign the trigger to the lattice with the `triggers` named keyword argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.lattice(triggers=dir_trigger)\n",
    "@ct.electron\n",
    "def my_workflow():\n",
    "    return \"File was changed.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Dispatch the `my_workflow` lattice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6e3a3e60-f105-48b9-95ad-fdff968a3f2f\n"
     ]
    }
   ],
   "source": [
    "dispatch_id = ct.dispatch(my_workflow)()\n",
    "# Show the dispatch ID\n",
    "print(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Monitor the Covalent UI. Watch the Dashboard for new dispatches of `my_workflow`.\n",
    "\n",
    "5. Make a change to the file: Add or remove a number and save the file.\n",
    "\n",
    "6. In the Covalent UI, observe that a new `my_workflow` is dispatched whenever you change `my_text_file.txt`.\n",
    "\n",
    "7. To stop the triggering behavior, use the `ct.stop_triggers` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "ct.stop_triggers(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No new dispatches are triggered on `my_workflow`. To demonstrate this, save another change to the `my_text_file.txt` file.\n",
    "\n",
    "Note that the `stop_triggers` function disables all triggers attached to the specified dispatch. \n",
    "\n",
    "## See Also\n",
    "\n",
    "[Adding a Time Trigger to a Lattice](./trigger_time.ipynb)\n",
    "\n",
    "[Adding a Database Trigger to a Lattice](./trigger_database.ipynb)\n",
    "\n",
    "[Adding a SQLite Trigger to a Lattice](./trigger_sqlite.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  },
  "vscode": {
   "interpreter": {
    "hash": "ffe78875ce1aa6161f50f6a6dec2555e7255bbdb44cc39b93c0dfc1daa8da522"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
